package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.FavoriteQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.model.Favorite;
import cn.org.eshow.model.Product;
import cn.org.eshow.model.User;
import cn.org.eshow.service.FavoriteManager;
import cn.org.eshow.service.ProductManager;
import cn.org.eshow.webapp.action.response.FavoriteResponse;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏API接口
 */
@AllowedMethods({"mine", "save", "delete"})
public class FavoriteAction extends ApiBaseAction<Favorite> {

    private static final long serialVersionUID = 1L;

    @Autowired
    private FavoriteManager favoriteManager;
    @Autowired
    private ProductManager productManager;

    private Favorite favorite = new Favorite();
    private List<Favorite> favorites = new ArrayList<Favorite>();
    private FavoriteQuery query = new FavoriteQuery(Boolean.TRUE);

    /**
     * 我的收藏列表
     */
    public void mine() {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        query.setUserId(user.getId());
        Page<Favorite> page = favoriteManager.search(query);
        favorites = page.getDataList();
        if (favorites.isEmpty()) {
            RenderUtil.noData();
            return;
        }
        List<FavoriteResponse> responses = new ArrayList<FavoriteResponse>();
        for (Favorite obj : favorites) {
            responses.add(new FavoriteResponse(obj));
        }
        RenderUtil.page("获取成功", "favorites", page, responses);
    }

    /**
     * 添加收藏信息
     */
    public void save() {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        if (favorite.getProduct() == null && favorite.getProduct().getId() == null) {
            RenderUtil.failure("参数不能为空");
            return;
        }
        Product product = productManager.get(favorite.getProduct().getId());
        if (product == null) {
            RenderUtil.failure("非法参数");
            return;
        }
        favorite.setProduct(product);
        favorite = favoriteManager.save(favorite, user);
        productManager.updateFavoriteSize(product, 1);
        RenderUtil.success("收藏成功");
    }

    /**
     * 删除收藏信息
     */
    public void delete() {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        if (favorite.getProduct() == null && favorite.getProduct().getId() == null) {
            RenderUtil.failure("参数不能为空");
            return;
        }
        Product product = productManager.get(favorite.getProduct().getId());
        favorite = favoriteManager.updateEnabled(favorite, user);
        productManager.updateFavoriteSize(product, -1);
        RenderUtil.success("取消收藏成功");
    }

    public Favorite getFavorite() {
        return favorite;
    }

    public void setFavorite(Favorite favorite) {
        this.favorite = favorite;
    }

    public List<Favorite> getFavorites() {
        return favorites;
    }

    public void setFavorites(List<Favorite> favorites) {
        this.favorites = favorites;
    }

    public FavoriteQuery getQuery() {
        return query;
    }

    public void setQuery(FavoriteQuery query) {
        this.query = query;
    }

}